﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblChildMenuDAO
    {
        private DBService _db = null;

        public TblChildMenuDAO(String connectionString)
        {
            _db = new DBService(connectionString);
        }

        public DataTable InUpChildMenu(TblChildMenu childMenu, int mode)
        {
            DataTable flag = null;
            try
            {
                flag = _db.ExeSP_DataTable("sp_TblChildMenu_InsUp",
                    new string[] { "@IDMenuChild", "@MenuChilName", "@MenuChilDescription", "@IDMenu_Parent", "@IDSupplie","@File", "@Mode" },
                    new object[] { childMenu.IDMenuChild, childMenu.MenuChilName, childMenu.MenuChilDescription, childMenu.IDMenu_Parent,  childMenu.IDSupplie, childMenu.File, mode }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetMenuChild()
        {
            DataTable Menu = null;
            try
            {
                Menu = _db.ExeSP_DataTable("sp_TblChildMenu_Get", false, CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Menu;
        }

        public DataTable GetMenuChildByIDParent(int IDMenu_Parent)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblChildMenu a  where a.IDMenu_Parent = @IDMenu_Parent",
                    new string[] { "@IDMenu_Parent" },
                    new object[] { IDMenu_Parent },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetMenuChildByIDChild(int IDMenuChild)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblChildMenu a  where a.IDMenuChild = @IDMenuChild",
                    new string[] { "@IDMenuChild" },
                    new object[] { IDMenuChild },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public bool DeleteChildMenu(TblChildMenu ChildMenu)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete TblChildMenu where IDMenuChild = @IDMenuChild",
                    new string[] { "@IDMenuChild" },
                    new object[] { ChildMenu.IDMenuChild },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public TblChildMenu GetMenuChildByID(int IDMenuChild)
        {
            TblChildMenu ChildMenu = new TblChildMenu();
            try
            {
                DataTable Menu = null;

                Menu = _db.ExeSP_DataTable("select * from TblChildMenu where IDMenuChild = @IDMenuChild",
                    new string[] { "@IDMenuChild" },
                    new object[] { IDMenuChild }
                        , CommandType.Text);
                foreach (DataRow dr in Menu.Rows)
                {
                    ChildMenu.IDMenuChild = IDMenuChild;
                    ChildMenu.IDMenu_Parent = Convert.ToInt32(dr["IDMenu_Parent"].ToString());
                    ChildMenu.IDSupplie = Convert.ToInt32(dr["IDSupplie"].ToString());
                    ChildMenu.MenuChilDescription = dr["MenuChilDescription"].ToString();
                    ChildMenu.MenuChilName = dr["MenuChilName"].ToString();
                    ChildMenu.File = dr["File"].ToString();


                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ChildMenu;
        }

    }
}
